package com.cloudsigns.factorymanager2.produce.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceEndHandoverFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceHandoverStatusFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceWorkpartReturnFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

public class ProduceWorkpartHandoverActivity extends FragmentActivity implements View.OnClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_handover_status, ll_return_processing, ll_end_handover;
    private TextView tv_handover_status, tv_return_processing, tv_end_handover;
    private CustomViewPager viewpager;
    public static final int STATUS = 0;
    public static final int RETURN = 1;
    public static final int END = 2;
    public int currentIndex = STATUS;

    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    private ProduceHandoverStatusFragment handoverStatusFragment;
    private ProduceWorkpartReturnFragment returnProcessingFragment;
    private ProduceEndHandoverFragment endHandoverFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_produce_workpart_handover);

        init();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        ll_handover_status = findViewById(R.id.ll_handover_status);
        ll_return_processing = findViewById(R.id.ll_return_processing);
        ll_end_handover = findViewById(R.id.ll_end_handover);
        tv_handover_status = findViewById(R.id.tv_handover_status);
        tv_return_processing = findViewById(R.id.tv_return_processing);
        tv_end_handover = findViewById(R.id.tv_end_handover);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);

        ll_handover_status.setOnClickListener(this);
        ll_return_processing.setOnClickListener(this);
        ll_end_handover.setOnClickListener(this);

        tv_title.setText(getResources().getString(R.string.process_handover));

        handoverStatusFragment = new ProduceHandoverStatusFragment();
        returnProcessingFragment = new ProduceWorkpartReturnFragment();
        endHandoverFragment = new ProduceEndHandoverFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(handoverStatusFragment);
        listFragments.add(returnProcessingFragment);
        listFragments.add(endHandoverFragment);
        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case STATUS:
                ll_handover_status.setSelected(true);
                ll_return_processing.setSelected(false);
                ll_end_handover.setSelected(false);

                tv_handover_status.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_return_processing.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_end_handover.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;

            case RETURN:
                ll_handover_status.setSelected(false);
                ll_return_processing.setSelected(true);
                ll_end_handover.setSelected(false);

                tv_handover_status.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_return_processing.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_end_handover.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;

            case END:
                ll_handover_status.setSelected(false);
                ll_return_processing.setSelected(false);
                ll_end_handover.setSelected(true);

                tv_handover_status.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_return_processing.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_end_handover.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
            default:
                break;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 交接状态
            case R.id.ll_handover_status:
                if (currentIndex != STATUS) {

                    currentIndex = STATUS;
                    viewpager.setCurrentItem(STATUS);
                    showSelectedOption(STATUS);
                }
                break;
            // 退回处理
            case R.id.ll_return_processing:
                if (currentIndex != RETURN) {

                    currentIndex = RETURN;
                    viewpager.setCurrentItem(RETURN);
                    showSelectedOption(RETURN);
                }
                break;
            // 完结交接
            case R.id.ll_end_handover:
                if (currentIndex != END) {

                    currentIndex = END;
                    viewpager.setCurrentItem(END);
                    showSelectedOption(END);
                }
                break;
        }
    }
}
